package com.lhs.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lhs.Result.Result;
import com.lhs.entity.blog.blog_comment;
import com.lhs.mapper.blog_post_0Mapper;
import com.lhs.service.blog_commentService;
import com.lhs.mapper.blog_commentMapper;
import com.lhs.service.blog_post_0Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author 15977
* @description 针对表【blog_comment(评论表)】的数据库操作Service实现
* @createDate 2025-09-11 09:04:15
*/
@Service
public class blog_commentServiceImpl extends ServiceImpl<blog_commentMapper, blog_comment>
    implements blog_commentService{



    @Autowired
    private blog_post_0Mapper blogPostMapper;

 @Override
public Result<blog_comment> getBlogComment(Long postId) {
    // 参数校验
    if (postId == null || postId <= 0) {
        return Result.error("参数错误");
    }

    try {
        // 使用oneOpt方法避免多条数据异常，同时处理可能的多结果情况
        blog_comment blogComment = this.getOne(new QueryWrapper<blog_comment>().eq("post_id", postId));
        if (blogComment == null) {
            return Result.success(null);
        }

        return Result.success(blogComment);
    } catch (Exception e) {
        // 处理数据库异常等运行时异常
        return Result.error("查询评论失败");
    }
}

@Override
public Result<String> addBlogComment(Long postId, blog_comment blogComment) {
    // 参数校验
    if (postId == null || postId <= 0) {
        return Result.error("参数错误");
    }
    if (blogComment == null) {
        return Result.error("评论内容不能为空");
    }

    String content = blogComment.getContent();
    if (content == null || content.trim().isEmpty()) {
        return Result.error("内容不能为空");
    }

    // 1. 校验用户身份
    if (!StpUtil.isLogin()) {
        return Result.error("请先登录");
    }

    // 构造保存对象
    blog_comment commentToSave = new blog_comment();
    commentToSave.setUserId(StpUtil.getLoginIdAsLong());
    commentToSave.setContent(content.trim());
    commentToSave.setPostId(postId);

    // 设置 parentId，默认为 0 表示顶级评论
    Long parentId = blogComment.getParentId();
    commentToSave.setParentId(parentId != null ? parentId : 0L);

    // 3. 保存评论
    try {
        boolean save = this.save(commentToSave);
        if (save) {
            return Result.success("评论成功");
        } else {
            return Result.error("评论失败");
        }
    } catch (Exception e) {
        // 异常处理：记录日志并返回错误信息
        log.error("保存评论失败", e);
        return Result.error("系统异常，请稍后再试");
    }
}

// 删除评论
  @Override
public Result<String> deleteBlogComment(Long commentId) {

    if (commentId == null || commentId <= 0) {
        return Result.error("参数错误");
    }

    blog_comment comment = this.getById(commentId);
    if (comment == null) {
        return Result.error("评论不存在");
    }

    Long userId = StpUtil.getLoginIdAsLong();

    if (!Objects.equals(comment.getUserId(), userId) && !blogPostMapper.checkUserBlogRelationExists(userId, comment.getPostId())) {
        return Result.error("没有权限删除此评论");
    }

    try {
        return this.removeById(commentId) ? Result.success("删除成功") : Result.error("删除失败");
    } catch (Exception e) {
        return Result.error("删除失败");
    }
}



}




